package net.pingfang.devicelock;



import lombok.extern.slf4j.Slf4j;
import net.pingfang.devicelock.core.DeviceGpsService;
import net.pingfang.model.annotation.Log;
import net.pingfang.model.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/device-lock")
@Slf4j
public class DeviceLockApi {
    @Autowired
    private DeviceGpsService deviceGpsService;

    @Autowired
    private NettyClientService nettyClientService;

    //身份判断
    @Log(operate = "权限验证")
    @GetMapping("/shior")
    public Result<String> shior(){
        return Result.success("成功");
    }

    /**
     * 向电子锁发送报文 必须指定设备号
     * 电子锁每隔段时间会向主服务器发送当前位置或 lbs定位/卫星定位(数据中包括设备号)
     *
     * 向电子锁发送施封 解封指令 ，电子锁会回传数据、响应值(1/0)、流水号(在指令中包括的流水号)、
     */
    @Log(operate = "获取电子锁GPS位置")
    @GetMapping("/queryGps")
    public void queryGps(){

    }

    @Value("${device.lock.seal}")
    private short seal;

    @Value("${device.lock.unseal}")
    private short unseal;

    //设备id
    @Log(operate = "解封电子锁")
    @PostMapping("/unseal")
    public void unseal_device(@RequestParam("code") String device_code){
        deviceGpsService.Send_Seal(device_code, unseal);
    }

    @Log(operate = "上位机发送施封命令到阅读器")
    @PostMapping("/sealLock")
    public void seal_Lock(@RequestParam(name="name") String LockName,
                          @RequestParam(name="ip") String ip,
                              @RequestParam(name="password") String PassWord ) throws Exception {
//        deviceGpsService.NettyConnect(LockName, PassWord);
        nettyClientService.sendBytes(LockName,PassWord);
    }




    @Log(operate = "施封电子锁")
    @PostMapping("/seal")
    public void seal_device(@RequestParam("device_code") String device_code){
        deviceGpsService.Send_Seal(device_code, seal);
    }

    @Log(operate = "获取设备参数")
    @PostMapping("/query-param")
    public void query_params(@RequestParam("device_code") String device_code){
        deviceGpsService.Send_Seal(device_code, seal);
    }

    //后端主动发送数据到前端
//    @GetMapping(path = "/sse/data", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public Flux<String> streamData() {
//        return Flux.interval(Duration.ofSeconds(1))
//                .map(sequence -> "数据内容-" + sequence);
//    }


    @Log(operate = "判断电子锁在线离线")
    @PostMapping("/is-live")
    public void is_live(){


    }





}
